function init() {
    Aid = $("#username").data("spaid");
    var username = $("#username").data("username");
    var appName = $("#special-appname");
    $.post("/ajax/getFollow", {username: username}, function (json) {
        var res = $.parseJSON(json);
        var total = res.total;
        var follows = res.follows;
        var followApp;
        for (i = 0; i < total; i++) {
            if (Aid === follows[i].AID) {
                appName.text(follows[i].name);
            }
        }
    });
}

function initRecent(myChart) {
    // 基于准备好的dom，初始化echarts实例
    // var myChart = echarts.init(document.getElementById('recent'));
    // 指定图表的配置项和数据
    var option = {
        tooltip: {
            trigger: 'axis'
        },
        legend: {
            data: ['评论增长数']
        },
        grid: {
            left: '3%',
            right: '4%',
            bottom: '3%',
            containLabel: true
        },
        xAxis: {
            type: 'category',
            boundaryGap: false,
            data: ['周一', '周二', '周三', '周四', '周五', '周六', '周日']
        },
        yAxis: {
            type: 'value'
        },
        series: [
            {
                name: '评论增长数',
                type: 'line',
                stack: '总量',
                data: [120, 132, 101, 134, 90, 230, 210]
            }
        ]
    };

    // 使用刚指定的配置项和数据显示图表。
    myChart.setOption(option);
}

function queryRecent(myChart) {
    var startDate = $("#startDate").val();
    var endDate = $("#endDate").val();
    myChart.showLoading();
    $.post("/ajax/getCommentCount", {startDate: startDate, endDate: endDate, Aid: Aid}, function (json) {
        var data = $.parseJSON(json);
        myChart.hideLoading();
        myChart.setOption({
            xAxis: {
                type: 'category',
                boundaryGap: false,
                data: data.day
            },
            series: [
                {
                    name: '评论增长数',
                    type: 'line',
                    stack: '总量',
                    data: data.count
                }
            ]
        });
    });
}

function initLike(myChart) {
    // var myChart = echarts.init(document.getElementById('like'));
    // 指定图表的配置项和数据
    var option = {
        tooltip: {
            trigger: 'item',
            formatter: "{a} <br/>{b}: {c} ({d}%)"
        },
        legend: {
            orient: 'vertical',
            x: 'left',
            data: ['一星', '二星', '三星', '四星', '五星']
        },
        series: [
            {
                name: '喜好程度',
                type: 'pie',
                radius: ['50%', '70%'],
                avoidLabelOverlap: false,
                label: {
                    normal: {
                        show: false,
                        position: 'center'
                    },
                    emphasis: {
                        show: true,
                        textStyle: {
                            fontSize: '30',
                            fontWeight: 'bold'
                        }
                    }
                },
                labelLine: {
                    normal: {
                        show: false
                    }
                },
                data: [
                    {value: 335, name: '一星'},
                    {value: 310, name: '二星'},
                    {value: 234, name: '三星'},
                    {value: 135, name: '四星'},
                    {value: 1548, name: '五星'}
                ]
            }
        ]
    };
    // 使用刚指定的配置项和数据显示图表。
    myChart.setOption(option);
}

function queryLike(myChart) {
    var startDate = $("#startDate").val();
    var endDate = $("#endDate").val();
    myChart.showLoading();
    $.post("/ajax/getRankCount", {startDate: startDate, endDate: endDate, Aid: Aid}, function (json) {
        var data = $.parseJSON(json);
        myChart.hideLoading();
        myChart.setOption({
            tooltip: {
                trigger: 'item',
                formatter: "{a} <br/>{b}: {c} ({d}%)"
            },
            legend: {
                orient: 'vertical',
                x: 'left',
                data: ['一星', '二星', '三星', '四星', '五星']
            },
            series: [
                {
                    data: data
                }
            ]
        });
    });
}

function changeEcharts() {
    var datepicker = $(".datepicker");

    var recent = echarts.init(document.getElementById('recent-1'));
    var like = echarts.init(document.getElementById('like-1'));

    initRecent(recent);
    initLike(like);

    queryRecent(recent);
    queryLike(like);

    datepicker.change(function () {
        $(".datepicker-a").removeClass("actived");
        queryRecent(recent);
        queryLike(like);
        datepicker.eq(0).datepicker("option","maxDate",$("#endDate").val());
    });
}
function dateToStr(times) {
    var date = new Date(times);
    var year = date.getFullYear().toString();
    var month = (date.getMonth() + 1).toString();
    var day = date.getDate().toString();
    if (month.length === 1) {
        month = "0" + month;
    }
    if (day.length === 1) {
        day = "0" + day;
    }
    return year + "-" + month + "-" + day;
}
function datepicker() {
    var datepicker = $(".datepicker");
    var dateinput = $(".date");
    var start = $("#startDate");
    var end = $("#endDate");

    var date;
    var startDate;
    var endDate;

    datepicker.datepicker({
        changeMonth: true,
        changeYear: true,
        minDate: "-2Y",
        maxDate: 0,
        dateFormat: "yy-mm-dd"
    });


    dateinput.delegate(".datepicker-a", "click", function () {
        switch ($(this).text()) {
            case "最近一周": {
                date = new Date();
                startDate = dateToStr(date - 3600 * 1000 * 24 * 7);
                endDate = dateToStr(date);
                start.val(startDate);
                end.val(endDate);
                start.change();
                break;
            }
            case "最近一个月":{
                date = new Date();
                endDate = dateToStr(date);
                date.setMonth(date.getMonth()-1);
                startDate = dateToStr(date);
                start.val(startDate);
                end.val(endDate);
                start.change();
                break;
            }
            case "最近三个月":{
                date = new Date();
                endDate = dateToStr(date);
                date.setMonth(date.getMonth()-3);
                startDate = dateToStr(date);
                start.val(startDate);
                end.val(endDate);
                start.change();
                break;
            }
            case "最近一年":{
                date = new Date();
                endDate = dateToStr(date);
                date.setMonth(date.getMonth()-12);
                startDate = dateToStr(date);
                start.val(startDate);
                end.val(endDate);
                start.change();
                break;
            }
        }
        $(this).addClass("actived");
    });
}

$(function () {
    $.ajaxSetup({
        headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        }
    });
    init();
    datepicker();
    changeEcharts();
});
